rm(list = ls())

# Set Working Directory to Replication Folder
taskdir 		<- "~/Dropbox/final_hai_perlman_replication/"



setwd(taskdir)
library(tidyverse)
library(lfe)
require(data.table)
library(stringi)
library(MASS)
library(foreign) 

## load survey 1
survey1 <- read_csv(paste0(taskdir, 'input/survey1.csv'))

# Define all DVs
dvs <- c('tax_support' ,'politician_prevent' ,'politician_understand' ,
		'politician_advocate' ,'politician_sympathy'
		,'responsibility_local' ,'responsibility_federal' ,
		'responsibility_international')

# survey1%>%pull(read_climate_num)%>%table
survey1%>%filter(treat == 0 & republican==1)%>%pull(politician_sympathy)%>%sd(na.rm = T)
survey1%>%filter(republican == 1)%>%pull(cc_happening)%>%mean(na.rm = T)
survey1%>%filter(democrat == 1)%>%pull(cc_happening)%>%mean(na.rm = T)
survey1%>%filter(democrat == 0 &republican==0)%>%pull(cc_happening)%>%mean(na.rm = T)




## main results (with controls)
# Regression specification
rhs <- ' ~ treat+female+ white+ college|income|0 | 0'
# Run Regressions
outlist <- list()
for (dv in dvs) {
	fmla <- paste0(dv,rhs)%>%as.formula
	mod1 <- felm(fmla, data = survey1)
	mod2 <- felm(fmla, data = survey1%>%filter(republican == 1 ))
	mod3 <- felm(fmla, data = survey1%>%filter(democrat == 1 ))
	mod4 <- felm(fmla, data = survey1%>%filter(republican == 0 & democrat==0))

	# save models in list 
	templsit <- list(mod1, mod2, mod3, mod4)
	outlist[[dv]]  <- templsit

}
save(outlist, 
	file = paste0(taskdir, 'input/processing/regressions_survey1/ols_survey1_controls.RData'))



## main results (without controls)
# Regression specification
rhs_nocontrols <- ' ~ treat |0|0|0'
# Run Regressions
outlist <- list()
for (dv in dvs) {
	fmla <- paste0(dv,rhs_nocontrols)%>%as.formula
	mod1 <- felm(fmla, data = survey1)
	mod2 <- felm(fmla, data = survey1%>%filter(republican == 1 ))
	mod3 <- felm(fmla, data = survey1%>%filter(democrat == 1 ))
	mod4 <- felm(fmla, data = survey1%>%filter(republican == 0 & democrat==0))

	# save models in list 
	templsit <- list(mod1, mod2, mod3, mod4)
	outlist[[dv]]  <- templsit
}
save(outlist, 
 	file = paste0(taskdir, 'input/processing/regressions_survey1/ols_survey1_nocontrols.RData'))






## main results for subset with wildfire exposure
# Regression specification
rhs <- ' ~ treat+female+ white+ college|income|0 | 0'
# Run Regressions
outlist <- list()
for (dv in dvs) {
	fmla <- paste0(dv,rhs)%>%as.formula
	mod1 <- felm(fmla, data = survey1%>%filter((lost_home==1|know_someone==1)))
	mod2 <- felm(fmla, data = survey1%>%filter(republican == 1  & (lost_home==1|know_someone==1)))
	mod3 <- felm(fmla, data = survey1%>%filter(democrat == 1  & (lost_home==1|know_someone==1)))
	mod4 <- felm(fmla, data = survey1%>%filter(republican == 0 & democrat==0 & (lost_home==1|know_someone==1)))

	# save models in list 
	templsit <- list(mod1, mod2, mod3, mod4)
	outlist[[dv]]  <- templsit
}
save(outlist, 
	file = paste0(taskdir, 'input/processing/regressions_survey1/ols_survey1_subset_wildfireexposure.RData'))


## main results with additional control for personal exposure to wildfires
survey1<- survey1%>%mutate(exposure = (lost_home==1|know_someone==1))
# Regression specification
rhs_controls_exposure <- ' ~ treat+exposure +female+ white+ college|income|0 | 0'
# Run Regressions
outlist <- list()
for (dv in dvs) {
	fmla <- paste0(dv,rhs_controls_exposure)%>%as.formula
	mod1 <- felm(fmla, data = survey1)
	mod2 <- felm(fmla, data = survey1%>%filter(republican == 1 ))
	mod3 <- felm(fmla, data = survey1%>%filter(democrat == 1 ))
	mod4 <- felm(fmla, data = survey1%>%filter(republican == 0 & democrat==0))

	# save models in list 
	templsit <- list(mod1, mod2, mod3, mod4)
	outlist[[dv]]  <- templsit

}
save(outlist, 
	file = paste0(taskdir, 'input/processing/regressions_survey1/ols_survey1_control_wildfireexposure.RData'))











## main results for subset with belief in climate change
# Regression specification
rhs <- ' ~ treat+female+ white+ college|income|0 | 0'
# Run Regressions
outlist <- list()
for (dv in dvs) {
	fmla <- paste0(dv,rhs)%>%as.formula
	# Politician is a Republican
	mod1 <- felm(fmla, data = survey1%>%filter(cc_happening==1))
	mod2 <- felm(fmla, data = survey1%>%filter(republican == 1 &  cc_happening==1))
	mod3 <- felm(fmla, data = survey1%>%filter(democrat == 1 &  cc_happening==1))
	mod4 <- felm(fmla, data = survey1%>%filter(republican == 0 & democrat==0 & cc_happening==1))

	# save models in list 
	templsit <- list(mod1, mod2, mod3, mod4)
	outlist[[dv]]  <- templsit

}
save(outlist, 
	file = paste0(taskdir, 'input/processing/regressions_survey1/ols_survey1_subset_climatebeleif.RData'))



## main results for subset with knowledge of climate change
# Regression specification
rhs <- ' ~ treat+female+ white+ college|income|0 | 0'
# subset of respondents who read about CC or discuss with it with family atleast once a month
survey1<- survey1%>%mutate(knowledge = (discuss_climate_num %in% c(3,4)|read_climate_num %in% c(3,4)))
survey1%>%pull(knowledge)%>%table

# Run Regressions
outlist <- list()
for (dv in dvs) {
	fmla <- paste0(dv,rhs)%>%as.formula
	# Politician is a Republican
	mod1 <- felm(fmla, data = survey1%>%filter(knowledge==1))
	mod2 <- felm(fmla, data = survey1%>%filter(republican == 1 & knowledge==1))
	mod3 <- felm(fmla, data = survey1%>%filter(democrat == 1 & knowledge==1))
	mod4 <- felm(fmla, data = survey1%>%filter(republican == 0 & democrat==0 &  knowledge==1))

	# save models in list 
	templsit <- list(mod1, mod2, mod3, mod4)
	outlist[[dv]]  <- templsit

}
save(outlist, 
	file = paste0(taskdir, 'input/processing/regressions_survey1/ols_survey1_subset_climateknowledge.RData'))






## Main results with Ologit
rhs <- ' ~ treat +female+ income+ white+ college'

# Have to change all DVs to factor for Ordered logit
survey1<- survey1%>%
			mutate(tax_support = factor(tax_support, levels = 0:4),
				   politician_sympathy = factor(politician_sympathy,
				   								levels = 0:4))%>%
			mutate_at(all_of(c('politician_understand',
							   'politician_prevent',
							   'politician_advocate')),
				~factor(., levels = 0:2))%>%
			mutate_at(all_of(c('responsibility_local',
							   'responsibility_federal',
							   'responsibility_international')),
				~factor(., levels = 1:3))


# run models and save results
outlist <- list()
for (dv in dvs) {
	fmla <- paste0(dv,rhs)%>%as.formula
	mod1 <- polr(fmla,Hess=TRUE , data = survey1)
	mod2 <- polr(fmla,Hess=TRUE , data = survey1%>%filter(republican == 1))
	mod3 <- polr(fmla,Hess=TRUE , data = survey1%>%filter(democrat == 1))
	mod4 <- polr(fmla,Hess=TRUE , data = survey1%>%filter(republican == 0 & democrat==0))
	
	# save models in list 
	templsit <- list(mod1, mod2, mod3, mod4)
	outlist[[dv]]  <- templsit
}
save(outlist, 
	file = paste0(taskdir, 'input/processing/regressions_survey1/ols_survey1_ologit.RData'))



















